package com.cbl.v.ssczs.view.fragment;


import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.cbl.v.ssczs.mode.net.DataCallBack;
import com.cbl.v.ssczs.mode.util.PreferencesHelper;
import com.cbl.v.ssczs.mode.util.Tools;
import com.handmark.pulltorefresh.library.PullToRefreshBase;

import org.xutils.x;

import butterknife.ButterKnife;
import butterknife.Unbinder;


/**
 * A simple {@link Fragment} subclass.
 */
public abstract class BaseFragment extends Fragment implements View.OnClickListener,
        TextWatcher,DataCallBack,PullToRefreshBase.OnRefreshListener2 {
    private Context context;
    private LayoutInflater mInflater;
    private View view;
    private Unbinder unbinder;
    public PreferencesHelper preferencesHelper;
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        if (view == null) {
            context = getActivity();
            mInflater = LayoutInflater.from(context);
            /**
             * 加载布局
             */
            view = inflater.inflate(getLayout(), null);
            preferencesHelper=PreferencesHelper.get(context);
            x.view().inject(this,view);
            unbinder=ButterKnife.bind(this,view);
            /**
             * 获取所有的主件
             */
            initView(view);
            /**
             * 初始化数据
             */
            initData();
            /**
             * 设置监听事件
             */
            initListener();
        }
        return view;
    }

    @Override
    public void onClick(View view) {
        /**
         * 设置所有监听
         */
        onMyOnclick(view);
    }

    /**
     * 弹出toast
     *
     * @param info
     */
    protected void showInfo(String info) {
        Tools.ShowInfo(context,info);
    }

    protected abstract int getLayout();

    protected abstract void initView(View view);

    protected abstract void initData();

    protected abstract void initListener();

    protected abstract void onMyOnclick(View view);



    @Override
    public void beforeTextChanged(CharSequence s, int start, int count, int after) {

    }

    @Override
    public void onTextChanged(CharSequence s, int start, int before, int count) {

    }

    @Override
    public void afterTextChanged(Editable s) {

    }

    @Override
    public void onPullDownToRefresh(PullToRefreshBase refreshView) {

    }

    @Override
    public void onPullUpToRefresh(PullToRefreshBase refreshView) {

    }

    public void startNewActivity(Class<?> cls){
        startActivity(new Intent(getActivity(),cls));
    }



    public void startNewActivityWithData(Class<?> cls,String data){
        startActivity(new Intent(getActivity(),cls).putExtra("data",data));
    }




    /**
     * 网络请求的回调接口实现
     * @param result
     */

    @Override
    public void solve(String result) {

    }

    @Override
    public void dataBack(Object data, int order){

    }

    @Override
    public void errorBack(String error) {

    }

    @Override
    public void finishBack() {

    }

    @Override
    public void upDateApp(Object data) {

    }


    @Override
    public void onDestroy() {
        unbinder.unbind();
        super.onDestroy();
    }


}
